var mysql = require('mysql');
var superagent = require('superagent');

var msyqlConfig = require('./config').config.mysqlOptions;

let topic;

start();

function start() {
    let connection1 = mysql.createConnection(msyqlConfig);
    let query1 = `SELECT * FROM topic WHERE	flag = 0 and (follower_count>7590 or question_count>2422) ORDER BY	follower_count DESC LIMIT 1 offset 10;`;
    connection1.query(query1, function (err, res) {
        if (res) {
            let list = res;
            if (list.length > 0) {
                topic = list[0];
                update();
            } else {
                console.log('all end');
            }
        } else {
            console.log('query1 error');
            console.log(err);

        }
    });
    connection1.end();
}


let offset = 0;
let limit = 10;
let httpIndex = 0;
var countError = 0;

function update() {
    let topicId = topic.id;
    let topicName = topic.name;
    console.log();
    let url = `https://www.zhihu.com/api/v4/topics/${topicId}/feeds/essence?limit=${limit}&offset=${offset}`;
    superagent.get(url).end(function (err, res) {
        httpIndex += 1;
        console.log(`第${httpIndex}次请求`);
        console.log(topicId);
        console.log(topicName);
        // console.log(url);

        if (err) {
            console.log('请求失败');
            httpIndex -= 1;
            countError += 1;
            if (countError > 3) {
                process.stdout.write('\x07');
            }
            setTimeout(() => {
                update();
            }, 30000);
        } else {
            console.log('请求成功');
            let text = res.text;
            let o = JSON.parse(text);

            let paging = o.paging;
            let data = o.data;

            var is_end = paging.is_end;
            if (is_end) {
                console.log('end');
                nextTopic();
            } else {
                let list2 = [];
                data.forEach((v, i) => {
                    let target = v.target;
                    let question = target.question
                    if (question) {
                        let id = question.id;
                        let title = question.title;
                        let created = question.created;

                        let item1 = [id, title, created];
                        list2.push(item1);
                    }
                });
                if (list2.length > 0) {
                    let connection = mysql.createConnection(msyqlConfig);
                    connection.connect();

                    let query = 'insert ignore into question values ?';
                    connection.query(query, [list2], function (err, res) {
                        console.log(`正在插入第${httpIndex}批数据`);
                        // console.log(list2);

                        if (err) {
                            console.log('insert error');
                        } else {
                            console.log('insert success');
                            console.log(res);

                        }
                        console.log();
                    })
                    connection.end();
                } else {
                    console.log('no data');
                    nextTopic();
                }
                offset += 10;
                update();
            }
        }
    })
}

function nextTopic() {
    let topicId = topic.id;
    let topicName = topic.name;

    let query2 = 'update topic set flag=true where id=' + topicId;
    let connection = mysql.createConnection(msyqlConfig);
    connection.query(query2, function (err, res) {
        console.log(topicId);
        console.log(topicName);
        if (err) {
            console.log('query2 error');

        } else {
            console.log('query2 success');
            offset = 0;
            httpIndex = 0; countError=0;
            start();
        }
        console.log();
    });
    connection.end();
}